package com.zw.yzk.kabu.view.activity;


import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.activity.CaptureFragment;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseApplication;
import com.zw.yzk.kabu.constant.ActivityConstant;
import com.zw.yzk.kabu.data.network.entity.CheckQrEntity;
import com.zw.yzk.kabu.internal.component.DaggerScanQrCodeComponent;
import com.zw.yzk.kabu.internal.module.ActivityModule;
import com.zw.yzk.kabu.internal.module.CheckQrCodeModule;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.management.ToastManager;
import com.zw.yzk.kabu.model.CheckQrCodeModel;
import com.zw.yzk.kabu.presenter.ScanQrCodePresenter;
import com.zw.yzk.kabu.view.view.CheckQrCodeView;

import javax.inject.Inject;


/**
 * Created by zhanwei on 2017/3/15.
 */

public class ScanQrCodeActivity extends CaptureActivity implements CheckQrCodeView {

    private CaptureFragment captureFragment;
    @Inject
    ScanQrCodePresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initComponent();

        captureFragment = new CaptureFragment();
        captureFragment.setAnalyzeCallback(analyzeCallback);
        getSupportFragmentManager().beginTransaction().replace(com.uuzuche.lib_zxing.R.id.fl_zxing_container, captureFragment).commit();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.destroy();
        }
    }


    @Override
    public void checkSuccess(CheckQrCodeModel model) {
        if (captureFragment != null && captureFragment.getHandler() != null) {
            captureFragment.getHandler().sendEmptyMessageDelayed(com.uuzuche.lib_zxing.R.id.restart_preview, 1500);
        }
        Intent intent = new Intent(this, CheckTicketResultActivityActivity.class);
        intent.putExtra(ActivityConstant.CHECK_QR_CODE_RESULT, model);
        startActivity(intent);
    }

    @Override
    public void checkFailed(String reason) {
        if (captureFragment != null && captureFragment.getHandler() != null) {
            captureFragment.getHandler().sendEmptyMessageDelayed(com.uuzuche.lib_zxing.R.id.restart_preview, 1500);
        }
        CheckQrCodeModel model = new CheckQrCodeModel();
        model.setCheckResult(0);
        model.setCheckFailedReason(reason);
        Intent intent = new Intent(this, CheckTicketResultActivityActivity.class);
        intent.putExtra(ActivityConstant.CHECK_QR_CODE_RESULT, model);
        startActivity(intent);
    }

    /**
     * 注入
     */
    private void initComponent() {
        DaggerScanQrCodeComponent.builder()
                .applicationComponent(BaseApplication.getInstance().getApplicationComponent())
                .activityModule(new ActivityModule(this))
                .checkQrCodeModule(new CheckQrCodeModule())
                .build()
                .inject(this);
        presenter.setVeiw(this);
    }

    /**
     * 二维码解析回调函数
     */
    CodeUtils.AnalyzeCallback analyzeCallback = new CodeUtils.AnalyzeCallback() {
        @Override
        public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
            String[] tmp = result.split("\\|");
            if (tmp.length != 2) {
                //1.5秒后初始化扫描        //初始化扫描，可以进行再次扫描
                if (captureFragment != null && captureFragment.getHandler() != null) {
                    captureFragment.getHandler().sendEmptyMessageDelayed(com.uuzuche.lib_zxing.R.id.restart_preview, 1500);
                }
                ToastManager.getInstance().showToast(ScanQrCodeActivity.this, getResources().getString(R.string.check_qr_code_failed));
                return;
            }
            LoadingDialogManager.getInstance().showLoadingDialog(ScanQrCodeActivity.this);
            checkQrCode(tmp[0], tmp[1]);
        }

        @Override
        public void onAnalyzeFailed() {
            //1.5秒后初始化扫描
            if (captureFragment != null && captureFragment.getHandler() != null) {
                captureFragment.getHandler().sendEmptyMessageDelayed(com.uuzuche.lib_zxing.R.id.restart_preview, 1500);
            }
            ToastManager.getInstance().showToast(ScanQrCodeActivity.this, getResources().getString(R.string.check_qr_code_failed));
        }
    };

    /**
     * 初始化页面
     */
    private void initView() {
        ViewGroup parent = (ViewGroup) LayoutInflater.from(this).inflate(com.uuzuche.lib_zxing.R.layout.camera, null);
        setContentView(parent);

        View toolbar = LayoutInflater.from(this).inflate(R.layout.layout_navigation_bar, null);
        TextView title = (TextView) toolbar.findViewById(R.id.tool_bar_title);
        View back = toolbar.findViewById(R.id.back);
        ViewGroup.LayoutParams p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        toolbar.setLayoutParams(p);

        parent.addView(toolbar);

        title.setText(getResources().getString(R.string.scan_qr_code));
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /**
     * 检查二维码
     *
     * @param serialNumber
     * @param checkCode
     */
    private void checkQrCode(String serialNumber, String checkCode) {
        presenter.checkQrCode(serialNumber, checkCode);
    }

}